﻿@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Enums.Base
@using ZBJF.Warranty.Model
@using ZBJF.Warranty.WebProvider
@model ZBJF.Warranty.Model.ServiceProviderProduct
@{
    var id = Model != null ? Model.ID : 0;
}
<ul class="nav nav-tabs">
    <li><a href="/ServiceProviderProduct/ServiceProviderProductIndex">服务商产品列表</a></li>
    <li class="active"><a href="/ServiceProviderProduct/ServiceProviderProductEdit/0">添加服务商产品</a></li>
</ul>
@using (Html.BeginForm("SaveServiceProviderProduct", "ServiceProviderProduct", FormMethod.Post, new { id = "inputForm", name = "inputForm", @class = "form-horizontal" }))
{
    @Html.HiddenFor(m => m.ID)
    <div class="control-group">
        <label class="control-label">服务商产品名称：</label>
        <div class="controls">
            @Html.TextBoxFor(m => m.ThirdProductName, new { validate = "{required:true,messages:{required:'请输入服务商产品名称'}}", maxlength = 20 })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">服务商产品ID：</label>
        <div class="controls">
            @Html.TextBoxFor(m => m.ThirdProductId, new { validate = "{required:true,messages:{required:'请输入第三方服务商系统产品ID'}}", maxlength = 8, onkeyup = "onlyInputDecimal(this,0)" })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">单价：</label>
        <div class="controls">
            @Html.TextBoxFor(m => m.ThirdProductPrice, new { validate = "{required:true,messages:{required:'请输入单价'}}", maxlength = 8, onkeyup = "onlyInputDecimal(this, 2)" })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">所属服务商：</label>
        <div class="controls">
            @Html.DropDownListFor(m => m.ServiceProviderId, ConvertToSelector.Instance.ServiceProviderConvertToSelectList(), new { validate = "{required:true,messages:{required:'请选择所属服务商'}}" })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">服务项目类别：</label>
        <div class="controls">
            @Html.DropDownListFor(m => m.ServiceProductType, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ServiceProductType)), new { validate = "{required:true,messages:{required:'请选择服务项目类别'}}" })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">服务项目：</label>
        <div class="controls">
            @Html.DropDownListFor(m => m.ServiceProjectId, ConvertToSelector.Instance.ServiceProjectConvertToSelectList(Model!=null?Model.ServiceProductType:Convert.ToByte(0)), new { validate = "{required:true,messages:{required:'请选择服务项目'}}" })
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">服务项目单价：</label>
        <div class="controls">
            <input type="text" readonly value="@(ViewBag.serviceProjectPrice)" id="serviceProjectPrice" />
        </div>
    </div>
    <div class="control-group">
        <label class="control-label"></label>
        <div class="controls">
            <label>
                @Html.CheckBox("IsAvailable", Model == null ? true : Model.IsAvailable)
                是否可用
            </label>
        </div>
    </div>
        <div class="form-actions">
            <input id="btnSubmit" class="btn btn-primary Edit-R" type="button" value="保存" onclick="loading(this)">
            <input id="btnCancel" class="btn" type="button" value="重置" onclick="reset()">
        </div>
}

@section scripts{
    <script>
        $(function () {
            //选择服务项目带出项目的价格
            $("#ServiceProjectId").change(function () {
                let _value = $(this).val();
                if (!_value)
                {
                    $("#serviceProjectPrice").val("");
                    return;
                }
                $.get("/ServiceProject/GetServiceProjectPrice", { ServiceProjectId: _value }, function (data, textStatus) {
                    $("#serviceProjectPrice").val(data);
                });
            })
            //选择服务项目类型生成服务项目
            $("#ServiceProductType").change(function () {
                let _value = $(this).val();
                if (!_value) {
                    $("#ServiceProjectId").html("<option value>请选择</option>");
                    return;
                }
                $.get("/ServiceProject/GetServiceProject", { ServiceProductType: _value }, function (data, textStatus) {
                    console.log(data);
                    let _html = "<option value>请选择</option>";
                    if (!data || data.length.length<1)
                    {
                        layer.alert("获取服务项目失败,请重试");
                        $("#ServiceProjectId").html(_html);
                        return;
                    }
                    for (var i = 0, j = data.length; i < j; i++)
                    {
                        let item = data[i];
                        _html += '<option value="' + item.ID + '">' + item.ProjectName + '</option>';
                    }
                    $("#ServiceProjectId").html(_html);
                }, "json");
            })

        })
        function reset() {
            document.getElementById("inputForm").reset();
        }
        function loading(_this) {
            if ($("#inputForm").valid()) {
                var serviceProjectPrice=parseInt( $("#serviceProjectPrice").val()||0);
                if (serviceProjectPrice && serviceProjectPrice <= parseInt($("#ThirdProductPrice").val())) {
                    layer.confirm('服务项目单价小于或等于服务商产品单价，确定要提交吗？', {
                        btn: ['确定', '取消']//按钮
                    }, function (index) {
                        layer.close(index);
                        _this.disabled = 'disabled';
                        document.getElementById("inputForm").submit();
                    });
                } else {
                    _this.disabled = 'disabled';
                    document.getElementById("inputForm").submit();
                }

            }
        }
    </script>
}
